package com.rabbitmq.demo.controller;

import com.rabbitmq.demo.pojo.User;
import com.rabbitmq.demo.service.UserService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

@RestController
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/login/{email}/{password}")
    public boolean receive(@PathVariable String email,
                           @PathVariable String password, HttpSession session) throws UnsupportedEncodingException {

        User user = userService.findUserByEmail(email);
        if(user == null){
            return false;
        }

        if(!user.getPassword().equals(password)){
            return false;
        }

        session.setAttribute("userId",user.getId());

        return true;
    }


    @RequestMapping("/register/{email}/{password}")
    public boolean register(@PathVariable String email,
                           @PathVariable String password, HttpSession session) throws UnsupportedEncodingException {

        boolean register = userService.register(email, password);

        return register;
    }
}
